// 引入数据库
const { result } = require('@hapi/joi/lib/base')
const db = require('../db/index')
const { query } = require('express')

// 酒店轮播
exports.hotelImage = (req,res)=>{
  const sql = 'select * from hotel_Carousel'
  db.query(sql,(err,result)=>{
    if(err)return res.cc(err.message)
    if(result.length===0)return res.cc('没有数据!')
    res.send({
        status:0,
        message:'获取酒店轮播成功!',
        data:result
    })
  })
}

// 酒店数据
exports.HotelData = (req,res)=>{
  const sql = 'select hotel_ID,hotel_name,hotel_star,hotel_price,PriceReduction,hotel_Comment,favorable_comment,hotel_Scoring,image from hotel'
  db.query(sql,(err,result)=>{
    if(err)return res.cc(err.message)
      if(result.length===0)return res.cc('没有数据!')
      res.send({
          status:0,
          message:'获取酒店数据成功!',
          data:result
      })
  })
}

// 酒店精选榜单数据
exports.LIST = (req,res)=>{
  const sql = 'select * from  hotel_LIST'
  db.query(sql,(err,result)=>{
    if(err)return res.cc(err.message)
      if(result.length===0)return res.cc('没有数据!')
      res.send({
          status:0,
          message:'获取酒店精选榜单成功!',
          data:result
      })
  })
}

// 酒店用户搜索
exports.searchFor = (req,res)=>{
  const user = req.query
  const sql = 'select hotel_ID,hotel_name,hotel_star,hotel_price,PriceReduction,hotel_Comment,Level_type,favorable_comment,hotel_Scoring,image from hotel where market_id = (select market_id from market where market_name like ?) and Level_type=?'
  const sql2 = 'select hotel_ID,hotel_name,hotel_star,hotel_price,PriceReduction,hotel_Comment,favorable_comment,hotel_Scoring,image from hotel where market_id = (select market_id from market where market_name like ?)'
  if(user.Level_type!==''){
  db.query(sql,[`%${user.market_name}%`,user.Level_type],(err,result)=>{
    if(err)return res.cc(err.message)
    if(result.length===0)return res.cc('没有数据!')
      res.send({
          status:0,
          message:'获取酒店数据成功!',
          data:result
      })
  })
  }else{
    db.query(sql2,[`%${user.market_name}%`],(err,result)=>{
      if(err)return res.cc(err.message)
      if(result.length===0)return res.cc('没有数据!')
        res.send({
            status:0,
            message:'获取酒店数据成功!',
            data:result
        })
    })
  }
  
}


//酒店为你推荐数据
exports.recommend = (req,res)=>{
  const user = req.query.LIST_id
  const sql = 'select hotel_id,hotel_price,Hotel_sales,Hotel_Introduction,image from hotel'
  const sql2 = 'select hotel_id,hotel_price,Hotel_sales,Hotel_Introduction,image from hotel where LIST_id = ?'
    if(user!==undefined){
      db.query(sql2,user,(err,result)=>{
        if(err)return res.cc(err.message)
        if(result.length===0)return res.cc('没有数据!')
          res.send({
              status:0,
              message:'酒店推荐数据获取成功!',
              data:result
          })
      })
    }else{
      db.query(sql,(err,result)=>{
        if(err)return res.cc(err.message)
        if(result.length===0)return res.cc('没有数据!')
          res.send({
              status:0,
              message:'酒店推荐数据获取成功!',
              data:result
          })
      })
    }
}

// 酒店详情房间的图片
exports.RoomImage = (req,res)=>{
  const sqlStr = 'select image_id,image from room_image where hotel_ID = ?'
  db.query(sqlStr,req.query.hotel_ID,(err,result)=>{
    if(err)return res.cc(err.message)
    if(result.length===0)return res.cc('没有房间图片地址数据!')
    const sqlStr1 = 'select a.hotel_ID,a.hotel_name,b.LIST,a.hotel_Comment,a.Hotel_sales,a.hotel_star,a.Room_type_details,a.hotel_price from hotel a join hotel_LIST b on a.LIST_id=b.LIST_id where a.hotel_ID = ?'
     const image = result
     db.query(sqlStr1,req.query.hotel_ID,(err,result)=>{
      if(err)return res.cc(err.message)
      let details = result
      if(result.length===0)return res.cc('没有房间详情数据!')
        const sql = 'select Hotel_pictures_id,Hotel_Attention from Hotel_pictures where hotel_ID = ?'
        db.query(sql,req.query.hotel_ID,(err,result)=>{
          if(err)return res.cc(err.message)
          if(result.length===0)return res.cc('没有房间类型数据!')
            const result1 = result
          res.send({
              status:0,
              message:'酒店房间详情数据获取成功!',
              data:{
                details,
                image,
                result1
              }
          })
        })
     })
  })
}

// 酒店类别
exports.HotelLevel = (req,res)=>{
  const sql = 'select Level_type from hotel group by Level_type'
  db.query(sql,(err,result)=>{
    if(err)return res.cc(err.message)
      if(result.length===0)return res.cc('没有数据!')
      res.send({
          status:0,
          message:'获取酒店级别类型成功!',
          data:result
      })
  })
}